package entities;

import java.awt.*;
import java.util.Map;

/***
 * Author: Mr.Ma, Time: 2018/5/8/20:27
 * Description: 将
 */

public class Jiang extends BasePiece {
    public Jiang(int x, int y, Color color) {
        super(x, y, color);
        if (color == BasePiece.RED_PIECE) {
            this.pieceName = "帅";
        } else {
            this.pieceName = "将";
        }
    }

    @Override
    public void updateAvailablePoints(Map<Point, BasePiece> pointPieceMap) {
        availablePoints.clear();
        int x = getPosition().x;
        int y = getPosition().y;

        if (x == 4 && y == 9) {
            BasePiece piece1 = pointPieceMap.get(new Point(4, 8));
            if ((piece1 == null) || (piece1.getColor() != this.getColor())) {
                this.availablePoints.add(new AvailablePoint(4, 8));
            }
            BasePiece piece2 = pointPieceMap.get(new Point(3, 9));
            if ((piece2 == null) || (piece2.getColor() != this.getColor())) {
                this.availablePoints.add(new AvailablePoint(3, 9));
            }
            BasePiece piece3 = pointPieceMap.get(new Point(5, 9));
            if ((piece3 == null) || (piece3.getColor() != this.getColor())) {
                this.availablePoints.add(new AvailablePoint(5, 9));
            }
        }
        if (x == 4 && y == 8) {
            BasePiece piece1 = pointPieceMap.get(new Point(4, 7));
            if ((piece1 == null) || (piece1.getColor() != this.getColor())) {
                this.availablePoints.add(new AvailablePoint(4, 7));
            }
            BasePiece piece2 = pointPieceMap.get(new Point(4, 9));
            if ((piece2 == null) || (piece1.getColor() != this.getColor())) {
                this.availablePoints.add(new AvailablePoint(4, 9));
            }
            BasePiece piece3 = pointPieceMap.get(new Point(3, 8));
            if ((piece3 == null) || (piece3.getColor() != this.getColor())) {
                this.availablePoints.add(new AvailablePoint(3, 8));
            }
            BasePiece piece4 = pointPieceMap.get(new Point(5, 8));
            if ((piece4 == null) || (piece4.getColor() != this.getColor())) {
                this.availablePoints.add(new AvailablePoint(5, 8));
            }
        }
        if (x == 3 && y == 9) {
            BasePiece piece1 = pointPieceMap.get(new Point(3, 8));
            if ((piece1 == null) || (piece1.getColor() != this.getColor())) {
                this.availablePoints.add(new AvailablePoint(3, 8));
            }
            BasePiece piece2 = pointPieceMap.get(new Point(4, 9));
            if ((piece2 == null) || (piece2.getColor() != this.getColor())) {
                this.availablePoints.add(new AvailablePoint(4, 9));
            }
        }
        if (x == 3 && y == 7) {
            BasePiece piece1 = pointPieceMap.get(new Point(3, 8));
            if ((piece1 == null) || (piece1.getColor() != this.getColor())) {
                this.availablePoints.add(new AvailablePoint(3, 8));
            }
            BasePiece piece2 = pointPieceMap.get(new Point(4, 7));
            if ((piece2 == null) || (piece2.getColor() != this.getColor())) {
                this.availablePoints.add(new AvailablePoint(4, 7));
            }
        }
        if (x == 5 && y == 7) {
            BasePiece piece1 = pointPieceMap.get(new Point(5, 8));
            if ((piece1 == null) || (piece1.getColor() != this.getColor())) {
                this.availablePoints.add(new AvailablePoint(5, 8));
            }
            BasePiece piece2 = pointPieceMap.get(new Point(4, 7));
            if ((piece2 == null) || (piece2.getColor() != this.getColor())) {
                this.availablePoints.add(new AvailablePoint(4, 7));
            }
        }
        if (x == 5 && y == 9) {
            BasePiece piece1 = pointPieceMap.get(new Point(5, 8));
            if ((piece1 == null) || (piece1.getColor() != this.getColor())) {
                this.availablePoints.add(new AvailablePoint(5, 8));
            }
            BasePiece piece2 = pointPieceMap.get(new Point(4, 9));
            if ((piece2 == null) || (piece2.getColor() != this.getColor())) {
                this.availablePoints.add(new AvailablePoint(4, 9));
            }
        }
        if (x == 3 && y == 8) {
            BasePiece piece1 = pointPieceMap.get(new Point(3, 7));
            if ((piece1 == null) || (piece1.getColor() != this.getColor())) {
                this.availablePoints.add(new AvailablePoint(3, 7));
            }
            BasePiece piece2 = pointPieceMap.get(new Point(3, 9));
            if ((piece2 == null) || (piece2.getColor() != this.getColor())) {
                this.availablePoints.add(new AvailablePoint(3, 9));
            }
            BasePiece piece3 = pointPieceMap.get(new Point(4, 8));
            if ((piece3 == null) || (piece3.getColor() != this.getColor())) {
                this.availablePoints.add(new AvailablePoint(4, 8));
            }
        }
        if (x == 5 && y == 8) {
            BasePiece piece1 = pointPieceMap.get(new Point(5, 7));
            if ((piece1 == null) || (piece1.getColor() != this.getColor())) {
                this.availablePoints.add(new AvailablePoint(5, 7));
            }
            BasePiece piece2 = pointPieceMap.get(new Point(5, 9));
            if ((piece2 == null) || (piece2.getColor() != this.getColor())) {
                this.availablePoints.add(new AvailablePoint(5, 9));
            }
            BasePiece piece3 = pointPieceMap.get(new Point(4, 8));
            if ((piece3 == null) || (piece3.getColor() != this.getColor())) {
                this.availablePoints.add(new AvailablePoint(4, 8));
            }
        }
        for (int yi = y - 1; yi >= 0; yi--) {
            BasePiece piece = pointPieceMap.get(new Point(x, yi));
            if (piece == null) {
                continue;
            }
            if (!piece.getPieceName().equals("将") || piece.getPieceName().equals("帅"))
                break;
            if (piece.getColor() != this.getColor() && (piece.getPieceName().equals("将")
                    || piece.getPieceName().equals("帅")))
                this.availablePoints.add(new AvailablePoint(x, yi));
        }

    }
}
